class Solution {
public:
    vector<int> avoidFlood(vector<int>& rains) {
        vector<int> ret(rains.size(), 1);
        set<int> se;
        unordered_map<int, int> hash;
        for (int i = 0; i < rains.size(); i++) {
            if (rains[i] == 0) se.insert(i);
            else {
                ret[i] = -1;
                if (hash.find(rains[i]) != hash.end()) {
                    auto it = se.lower_bound(hash[rains[i]]);
                    if (it == se.end()) return {};
                    ret[*it] = rains[i];
                    se.erase(it);
                }
                hash[rains[i]] = i;
            }
        }
        return ret;
    }
};